import sys
import tkinter
from tkinter.messagebox import showinfo
from tkinter import filedialog
from PIL import ImageTk

from knn_work import main as knn_main
from dl import neuralNetwork as nl_main

image_file_name = "temp.png"


# 打开文件对话框
def getfile():
    global image_file_name
    file_path = filedialog.askopenfilename()
    print(file_path)
    if file_path != () and file_path != "" and file_path is not None:
        image_file_name = file_path
        file = ImageTk.PhotoImage(file=image_file_name)
        image_label = tkinter.Label(image=file)
        image_label.image = file
        image_label.grid(row=1, column=1, columnspan=5, pady=20)
    pass


def show_windows_info(title, message):
    showinfo(title, message)


def show_hello():
    show_windows_info("hello", "hello world!")



def draw_windows(window):
    title_label = tkinter.Label(window, text="邮政编码数字识别")
    title_label.grid(row=0, column=0, columnspan=7, pady=20)

    file = ImageTk.PhotoImage(file=image_file_name)
    image_label = tkinter.Label(image=file)
    image_label.image = file
    image_label.grid(row=1, column=1, columnspan=5, pady=20)

    load_file_button = tkinter.Button(window, text="导入文件", padx=20, command=getfile)
    load_file_button.grid(row=2, column=0, columnspan=2)
    knn_button = tkinter.Button(window, text="knn识别", padx=20, command=knn_run)
    knn_button.grid(row=2, column=2, columnspan=2)
    dl_button = tkinter.Button(window, text="dl识别", padx=20, command=dl_run)
    dl_button.grid(row=2, column=4, columnspan=2)


def knn_run():
    show_windows_info("knn_work", knn_main.knnrun(image_file_name))


def dl_run():
    show_windows_info("dl_work", nl_main.neuralnetwork(image_file_name))


def main():
    # 创建一个窗口
    windows = tkinter.Tk()

    # 写标题
    windows.title("demo")

    # 设置窗口大小
    windows.geometry("1060x500")

    draw_windows(windows)

    # 进入消息循环
    windows.mainloop()


if __name__ == '__main__':
    main()
